

package jetbrains.buildServer.server.rest.model;

import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlRootElement;
import jetbrains.buildServer.server.rest.data.finder.impl.BuildPromotionFinder;
import jetbrains.buildServer.server.rest.model.build.Builds;
import jetbrains.buildServer.server.rest.swagger.annotations.ModelDescription;
import jetbrains.buildServer.server.rest.util.BeanContext;
import jetbrains.buildServer.server.rest.util.ValueWithDefault;
import org.jetbrains.annotations.NotNull;

/**
 * @author Yegor.Yarko
 * Date: 11/11/2017
 */
@SuppressWarnings("PublicField")
@XmlRootElement(name = "related")
@ModelDescription("Represents a link to the Builds entity.")
public class Related {
  @XmlElement
  public Builds builds;

  public Related() {
  }

  public Related(@NotNull final Fields fields, @NotNull final BeanContext beanContext) {
    final BuildPromotionFinder promotionFinder = beanContext.getSingletonService(BuildPromotionFinder.class);

    builds = ValueWithDefault.decideDefault(
      fields.isIncluded("builds", true, true),
      () -> {
        Fields nestedFields = fields.getNestedField("builds", Fields.SHORT, Fields.LONG);
        return Builds.createFromPrefilteredBuildPromotions(promotionFinder.getLazyResult(), nestedFields, beanContext);
      }
    );
  }
}